﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharePoint.Security.ConfigAnalyzer.Client.Views;
using System.ComponentModel;
using SharePoint.Security.ConfigAnalyzer.Engine.Common;
using System.IO;
using System.Reflection;

namespace SharePoint.Security.ConfigAnalyzer.Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        BackgroundWorker worker;
        public MainWindow()
        {
            //if (DateTime.Now > File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).AddDays(10))
            //{
            //    MessageBox.Show("This copy of tool is expired. Please get a new copy of the tool.", "Sharepoint Configuration Scanner - Expired", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            //    Environment.Exit(1);
            //}

            InitializeComponent();
            worker = new BackgroundWorker();
            worker.DoWork += worker_DoWork;
            worker.WorkerReportsProgress = true;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            MainProgress.Visibility = Visibility.Visible;
            worker.RunWorkerAsync();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            ObjectHelper.PreFetch();
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {

                this.ProgressPanel.Visibility = System.Windows.Visibility.Collapsed;
                if (e.Error != null)
                {
                    // Log e.Error
                    MessageBox.Show(string.Format(
                        "Oops! we encountered an error performing the operation. Please check if you have required permissions. {0} Error: {1}", 
                        Environment.NewLine
                        ,e.Error.Message),
                       "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                    Environment.Exit(0);
                }

                var summaryControl = new FarmSummary();
                var rulesControl = new Rules();
                this.SummaryContentControl.Content = summaryControl;
                this.RulesContentControl.Content = rulesControl;
                this.panel1.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                // Log error
                MessageBox.Show(string.Format(
                    "Oops! we encountered an error performing the operation. Please check if you have required permissions. {0} Error: {0}", Environment.NewLine ,ex.Message),
                   "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BtnInfo_Click(object sender, RoutedEventArgs e)
        {
            
                
        }
    }
}
